<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    <meta charset="UTF-8">
    <title>过滤相似单词</title>
    <style type="text/css">
        .newWord{
            font-style:italic;
            text-underline-position: auto;
            color: brown;
        }
    </style>
    <script type="text/javascript" src="js/jquery.min.js"></script>
    <script type="text/javascript" src="js/md5.js"></script>
    <script type="text/javascript">
        "use strict";

        function load() {

        }
        var arrOldWord = new Array;
        var arrNewWord = new Array;
        var arrTotal = new Array;
        var sameShort = new Array;
        var sameLong = new Array;

        function init() {
            var text01 = document.getElementById("area01").value;
            arrOldWord = text01.split("\n");
            var text02 = document.getElementById("area02").value;
            arrNewWord = text02.split("\n");
            //去除数组中的空项
            for (var i = arrNewWord.length; i > -1; i--) {
                if (arrNewWord[i] == "") {
                    arrNewWord.splice(i, 1);
                }
            }
            for (var i = arrOldWord.length; i > -1; i--) {
                if (arrOldWord[i] == "") {
                    arrOldWord.splice(i, 1);
                }
            }
            //检查内部是否有重复项
            for (var i = arrNewWord.length; i > 1; i--) {
                for (var j = i - 1; j > -1; j--) {
                    if (arrNewWord[i]== arrNewWord[j]) {
                        arrNewWord.splice(i, 1);
                        break;
                    }
                }
            }

            //检查两个数组是否有重复项，如果有，则删除arrNewWord的内容
            for (var i = arrNewWord.length - 1; i > -1; i--) {
                if (arrOldWord.indexOf(arrNewWord[i]) > -1) {
                    arrNewWord.splice(i, 1);
                }
            }
        }

        function btnok() {

            init();
            isSame();
            delSame();
            delOldSame();
            showSame();
            formatSame();
            showNotSame();
            console.log(sameShort);
            console.log(sameLong);
            console.log(arrNewWord.length);

        }
        //格式化显示相似表格，确定哪些单词来自于新词汇
        function formatSame(){

        }
        //如果相似的单词都是来自于旧词汇，也就没必要再分析了，所以要移除全部来源于旧词汇的结果
        function delOldSame() {
            for (var i = sameShort.length - 1; i > -1; i--) {
                if ((arrOldWord.indexOf(sameShort[i]) > -1) & (arrOldWord.indexOf(sameLong[i] > -1))) {
                    sameLong.splice(i, 1);
                    sameShort.splice(i, 1);
                }
            }
        }
        //显示不相同也不相似的单词，来源于area02
        function showNotSame() {
            var txtOut = "";
            for (var i = arrNewWord.length - 1; i > -1; i--) {
                if (sameLong.includes(arrNewWord[i]) || sameShort.includes(arrNewWord[i])) {
                    arrNewWord.splice(i, 1);
                } else {

                }
            }
            for (var i = 0; i < arrNewWord.length; i++) {
                txtOut += arrNewWord[i];
                txtOut += "\n";
            }
            document.getElementById("areaNotSame").value = txtOut;
        }
        //显示相似词的筛选结果
        function showSame() {
            for (var i = 0; i < sameShort.length; i++) {
                var tabSimilar = document.getElementById("tabSimilar");
                var row = tabSimilar.insertRow(tabSimilar.rows.length);
                var cell1 = row.insertCell(0);
                var cell2 = row.insertCell(1);
                var cell3 = row.insertCell(2);
                var cell4 = row.insertCell(3);
                if(arrNewWord.includes(sameShort[i])){
                    cell1.innerHTML = "<p class='newWord'>"+sameShort[i]+"</p>";
                    cell2.innerHTML="new";
                }else{
                    cell1.innerHTML = sameShort[i];
                }
                if(arrNewWord.includes(sameLong[i])){
                    cell3.innerHTML ="<p class='newWord'>"+ sameLong[i]+"</p>";
                    cell4.innerHTML="new";
                }else{
                    cell3.innerHTML = sameLong[i];
                }
               
            }
        }

        //去除相似词条检查中，完全相同的结果
        function delSame() {
            for (var i = sameShort.length - 1; i > -1; i--) {
                if (sameLong[i]== sameShort[i]) {
                    sameLong.splice(i, 1);
                    sameShort.splice(i, 1);
                }
            }
            console.log(sameShort);
            console.log(sameLong);
            for (var i = sameShort.length - 1; i > 0; i--) {
                for (var j = 0; j < i; j++) {
                    if ((sameShort[j] == sameShort[i]) & (sameLong[j] == sameLong[i])) {
                        sameShort.splice(i, 1);
                        sameLong.splice(i, 1);
                        //break;
                    }
                }
            }

        }

        //是否存在完全相同的单词
        function isSame() {

            var shortW = "";
            var longW = "";
            arrTotal = arrOldWord.concat(arrNewWord);
            for (var i = 1; i < arrTotal.length; i++) {
                for (var j = 0; j < i; j++) {
                    longW = "";
                    shortW = "";
                    if (arrTotal[i].length > arrTotal[j].length) {
                        longW = arrTotal[i];
                        shortW = arrTotal[j];
                    } else {
                        longW = arrTotal[j];
                        shortW = arrTotal[i];
                    }
                    // 确定了包含关系,p表示单词判断的最短长度，太短则没有判断的意义
                    // 例如of会包含在多个单词中
                    var p = 4;
                    if (shortW.length > p & longW.includes(shortW)) {

                        sameShort.push(shortW);
                        sameLong.push(longW);
                    }
                    // q是判断相似基本单元的长度
                    var q = 6;
                    if (shortW.length > q) {
                        isSimilar(shortW, longW, q);
                    }
                }
            }

        }

        // 检查是否有相似关系，把短的单词截取指定长度的字符，看长单词是否包含
        //str1是较短的单词，str2是较长的单词
        function isSimilar(str1, str2, l) {
            for (var k = 0; k < (str1.length - l); k++) {
                var subW = str1.substring(k, k + l);
                if (str2.includes(subW)) {

                    sameShort.push(str1);
                    sameLong.push(str2);
                }

            }
        }

        function testcode() {
            console.log(document.getElementById("tabSimilar").rows[1].cells[0].innerText);
            fanyi();
        }
        //调用百度翻译，异步的问题没有解决，所以没法用函数返回翻译结果，只能在内部直接把翻译结果赋值给文本框
        function fanyi() {
            for (var i = 0; i < 8; i++) {
                var intxt = document.getElementById("tabSimilar").rows[i].cells[0].innerText;
                var appid = '20180718000186694';
                var key = '6hlRV5ytiy0S_8QKQRMJ';
                var salt = 1435660288; //(new Date).getTime();
                var query = 'apple';
                // 多个query可以用\n连接  如 query='apple\norange\nbanana\npear'
                var from = 'en';
                var to = 'zh';
                var str1 = appid + query + salt + key;
                var sign = MD5(str1);
                //var intxt = document.getElementById("txt01").value;
                var inmd5 = appid + intxt + salt + key;
                var outmd5 = MD5(inmd5);
                var httpadrs = "https://api.fanyi.baidu.com/api/trans/vip/translate?q=" + intxt +
                    "&from=en&to=zh&appid=20180718000186694&salt=1435660288&sign=" + outmd5;
                var outtxt = 'false';
                $.ajax({
                    url: httpadrs,
                    type: 'GET',
                    dataType: 'JSONP',
                    async: false,
                    success: function (data) {
                        //提取json中的汉语翻译部分
                        outtxt = data.trans_result[0].dst;
                        console.log(outtxt);
                        document.getElementById("tabSimilar").rows[i].cells[1].innerText = outtxt;
                    }
                });
            }
        }
    </script>
</head>

<body onload="load()">
    <input type="button" onclick="testcode()" value="test">
    <div>
        <p>原有的词汇</p>
        <textarea id="area01" warp="virtual"></textarea>
    </div>
    <div>
        <p>待分析的词汇</p>
        <textarea id="area02" warp="virtual"></textarea>
    </div>

    <div>
        <input type="button" onclick="btnok()" value="ok">
        <input type="button" onclick="cleartxt()" value="clear">
    </div>
    <div>

    </div>
    <div>
        <p>相似的单词</p>
        <table id="tabSimilar" border="1" cellspacing="0">
            <tr>
                <td class="num">序号</td>
                <td class="timu">题目</td>
                <td class="daan">答案</td>
                <td class="timu">题目</td>

            </tr>
        </table>
    </div>
    <div>
        <p>不重复也不相似的单词</p>
        <textarea id="areaNotSame"></textarea>
    </div>



</body>

</html>